<?php
declare(strict_types=1);

namespace Modules\WeChat\Service;

use DB;
use HiCommon\Repository\AdminsRepository;
use HiCommon\Repository\RepairstoreGoodsRepository;
use HiCommon\Repository\RepairstoreGoodsImgRepository;
use HiCommon\Repository\RepairstoreTypeRepository;

class GoodsService extends BaseService
{
    /**
     * 获取门店商服务商品列表
     */
    public function goodsList($openid)
    {
        if (empty($openid)) {
            return hello_error('请登录');
        }

        $detail = AdminsRepository::get_shop_detail($openid);
        if ($detail) {
            $shop = $detail->toArray();
            if ($shop['shop']['cate'] == 2) {//维修店商品
                $list = self::getRepairstoreGoods($shop['shop']);

                return hello_success('获取成功', $list);
            } else { //检测站商品
                return hello_error('对不起检测站没有商品');
            }

        } else {
            return hello_error('门店不存在');
        }
    }


    /**
     * 获取维修店商品
     */
    public function getRepairstoreGoods($shop)
    {
        $res = RepairstoreGoodsRepository::api_get_list(['shop_id' => $shop['id']])->toArray();
        $list = array();
        if ($res['data']) {
            foreach ($res['data'] as $key => $value) {
                $list[] = [
                    'id' => $value['id'],
                    'img' => $value['pic'][0]['picurl']['full_src'],
                    'name' => $value['name'],
                    'shop_name' => $value['shop']['name'],
                    'shop_address' => $value['shop']['address'],
                    'price' => $value['price'],
                    'status' => $value['status']
                ];
            }
        }
        return list_page_data_for_api($res);
    }


    /**
     * 删除门店商品
     */
    public function delete($data)
    {
        try {
            if (empty($data['id'])) {
                return hello_error('服务商品id不能为空');
            }

            $res = RepairstoreGoodsRepository::delete_one((int)$data['id']);
            if ($res) {
                return hello_success('删除成功', []);
            }
            return hello_error('删除失败');

        } catch (\Exception $exception) {

            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 商品上下架
     */
    public function changeStatus($data)
    {
        try {
            if (empty($data['id'])) {
                return hello_error('服务商品id不能为空');
            }

            if (empty($data['status'])) {
                return hello_error('商品状态不能为空');
            }

            $res = RepairstoreGoodsRepository::change_one_state((int)$data['id'], (int)$data['status']);
            if ($res) {
                if($data['status'] == '1'){
                    return hello_success('上架成功', []);
                }
                return hello_success('下架成功', []);
            }
            return hello_error('设置失败');


        } catch (\Exception $exception) {

            return hello_error('失败', [], $exception->getMessage());
        }
    }


    /**
     * 创建维修店的商品提交数据验证
     */
    public function checkData($postData, $cate = false)
    {
        $openid = get_wat_token();
        if ($cate) {
            if (empty($openid)) {
                return ['code' => 1, 'smg' => '请登录'];
            } else {
                $detail = AdminsRepository::get_shop_detail($openid);
                if ($detail) {
                    $shop = $detail->toArray()['shop'];
                    $postData['repairstore_id'] = $shop['id'];
                    $postData['longitude'] = $shop['longitude'];
                    $postData['latitude'] = $shop['latitude'];
                } else {
                    return ['code' => 1, 'smg' => '门店不存在'];
                }
            }
        }else{
            if (empty($postData['id'])) {
                return ['code' => 1, 'smg' => '服务商品id不能为空'];
            }
        }
        if (empty($postData['name'])) {
            return ['code' => 1, 'smg' => '服务商品名称不能为空'];
        }
        if (empty($postData['type'])) {
            return ['code' => 1, 'smg' => '服务商品类型不能为空'];
        }
        if (empty($postData['price'])) {
            return ['code' => 1, 'smg' => '价格不能为空'];
        }
        if (empty($postData['price_indication'])) {
            return ['code' => 1, 'smg' => '价格说明不能为空'];
        }
        if (empty($postData['detail'])) {
            return ['code' => 1, 'smg' => '图文详情不能为空'];
        }
        if (empty($postData['fuwu'])) {
            return ['code' => 1, 'smg' => '售后服务不能为空'];
        }
        $imgArray = hi_array_del_null($postData['pic']);
        if (empty($imgArray)) {
            return ['code' => 1, 'smg' => '图片不能为空'];
        };

        unset($postData['pic']);

        return ['code' => 0, 'smg' => 'ok', 'goods' => $postData, 'imgArray' => $imgArray];
    }

    /**
     * 创建维修店的商品
     */
    public function create($postData)
    {
        try {
            $thisData = self::checkData($postData, true);

            if ($thisData['code'] == 1) {
                return hello_error($thisData['smg']);
            }

            $goods = RepairstoreGoodsRepository::add_one($thisData['goods']);
            if ($goods) {
                foreach ($thisData['imgArray'] as $key => $value) {
                    $img['goods_id'] = $goods->id;
                    $img['picurl'] = $value;
                    $img['sorts'] = $key + 1;
                    RepairstoreGoodsImgRepository::add_one($img);//存入图片信息
                }
            }

            return hello_success('添加成功', $goods);
        } catch (\Exception $exception) {

            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 获取门店商品详情
     */
    public function getDetail($n_id)
    {
        $n_id = (int)$n_id;
        if ($n_id <= 0) {
            hello_error('数据ID不能为空');
        }
        $res = RepairstoreGoodsRepository::get_one($n_id)->toarray();

        if ($res) {
            $detail = [
                'id' => $res['id'],
                'type' => $res['type'],
                'banner' => $res['pic'],
                'name' => $res['name'],
                'price' => $res['price'],
                'detail' => $res['detail'],
                'fuwu' => $res['fuwu'],
                'price_indication' => $res['price_indication'],
            ];

            return hello_success('查询成功', $detail);
        }

        return hello_error('查询失败', []);
    }

    /**
     * 编辑维修店的商品
     */
    public function update($postData)
    {
        try {
            $thisData = self::checkData($postData);

            if ($thisData['code'] == 1) {
                return hello_error($thisData['smg']);
            }

            $goods = RepairstoreGoodsRepository::edit_one($thisData['goods']);

            if ($goods) {
                $goods_id = (int)$thisData['goods']['id'];
                //删除原有数据
                RepairstoreGoodsImgRepository::delete_by_where(['goods_id' => $goods_id], true);
                $up_data = array();
                foreach ($thisData['imgArray'] as $key => $value) {
                    if($value){
                        $up_data[$key]['goods_id'] = $goods_id;
                        $up_data[$key]['picurl'] = $value;
                        $up_data[$key]['sorts'] = $key+1;
                    }
                }
                RepairstoreGoodsImgRepository::add_many($up_data);//存入图片信息
            }

            return hello_success('编辑成功', $goods);
        } catch (\Exception $exception) {

            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 获取服务商品分类
     */
    public function getType()
    {
        $list = RepairstoreTypeRepository::get_all(['status' =>1],['id','title']);
        if($list){
            return hello_success('获取成功', $list);
        }
        return hello_error('暂无数据', $list);
    }




}